package com.cpr.ams.web.controller;

import com.cpr.ams.bean.PayService;
import com.cpr.ams.bean.Room;
import com.cpr.ams.bean.extend.OrderExtend;
import com.cpr.ams.service.ICheckInService;
import com.cpr.ams.service.IRoomService;
import com.cpr.ams.util.Result;
import com.jacky.base.utils.PageVM;
import com.sun.istack.internal.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(description = "租房订单操作相关接口")
@Validated
@RestController
@RequestMapping("/checkin")
public class CheckInController {
    @Autowired
    private ICheckInService checkInService;
    @Autowired
    private IRoomService roomService;
    @ApiOperation(value = "插入或更新租房订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "time", value = "订单时间",paramType = "query",required = true),
            @ApiImplicitParam(name = "tenantId", value = "用户id",paramType = "query",required = true),
            @ApiImplicitParam(name = "roomId", value = "房间id",paramType = "query",required = true),
//            @ApiImplicitParam(name = "type", value = "订单类型，预约看房定金",paramType = "query",required = true),
            @ApiImplicitParam(name = "status", value = "订单状态，1已付定金、2已付租金,3退定金",paramType = "query",required = true),
            @ApiImplicitParam(name = "amount", value = "付款金额",paramType = "query",required = true),
            @ApiImplicitParam(name = "backup1", value = "开始时间",paramType = "query"),
            @ApiImplicitParam(name = "backup2", value = "结束时间",paramType = "query")
    })
    @PostMapping("/saveOrUpdateCheckIn")
    public Result saveOrUpdateCheckIn(Integer id, @NotNull String time, @NotNull Integer tenantId, @NotNull Integer roomId,  @NotNull Integer status,@NotNull String amount,String backup1,String backup2){
        PayService order=new PayService();
        order.setId(id);
        order.setTime(time);
        order.setTenantId(tenantId);
        order.setRoomId(roomId);
        order.setType("");
       order.setStatus(status);
       order.setAmount(amount);
       order.setBackup1(backup1);
       order.setBackup2(backup2);
       checkInService.saveOrUpdateCheck(order);
        System.out.println(status);
       if (id==null) {
           Room room = new Room();
           room.setRoomId(roomId);
           room.setStatus("已锁定");
           roomService.updateById(room);
       }else if(status==3){
           Room room = new Room();
           room.setRoomId(roomId);
           room.setStatus("未租住");
           System.out.println(room);
           roomService.updateById(room);
       }else if(status==2){
           Room room = new Room();
           room.setRoomId(roomId);
           room.setStatus("已租住");
           roomService.updateById(room);
       }
        return Result.success("插入或更新成功");
    }
    @ApiOperation(value = "分页查询租房订单信息")
    @GetMapping("/pageQuery")
    @ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query")
    })
    public Result pageQuery(@NotNull Integer page, @NotNull Integer pageSize, Integer userId) {
        PageVM<OrderExtend> pageVM = checkInService.query(page,pageSize,userId);
        return Result.success(pageVM);
    }
    @ApiOperation(value = "根据id查询订单信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query",required=true)
    )
    @GetMapping("/queryById")
    public Result queryById(@NotNull Integer id) {
        OrderExtend orderExtend=checkInService.queryById(id);
        return Result.success(orderExtend);
    }

}
